package com.app.teaware.bean;


import android.content.Context;
import android.text.TextUtils;

import com.app.teaware.basis.Constants;
import com.app.teaware.jpush.TagAliasOperatorHelper;
import com.clj.fastble.data.BleDevice;

import java.util.ArrayList;
import java.util.List;

import my.FileUtils;
import my.LogUtil;
import my.MySharedPreferences;


public class BleDeviceListBean extends BasisBean {
    private static final long serialVersionUID = 1L;


    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "bleDevice.data";
    private static BleDeviceListBean mBleDeviceBean;


    public void addBleDevice(BleDevice bleDevice){
        List<BleDevice> bleDeviceList = getBleDevices();
        if(!bleDeviceList.contains(bleDevice)){
            bleDeviceList.add(bleDevice);
        }
    }

    public void deleteBleDevice(BleDevice bleDevice){
        List<BleDevice> bleDeviceList = getBleDevices();
       bleDeviceList.remove(bleDevice);
    }

    public List<BleDevice> getBleDevices() {
        if(bleDevices == null){
            bleDevices = new ArrayList<>();
        }
        return bleDevices;
    }

    public void setBleDevices(List<BleDevice> bleDevices) {
        this.bleDevices = bleDevices;
    }

    private List<BleDevice> bleDevices;


    public BleDeviceListBean() {
        super();
        // mBleDeviceBean = this;
    }


    public static BleDeviceListBean getInstance() {
        if (mBleDeviceBean == null)
            mBleDeviceBean = getFromCache();
        return mBleDeviceBean;
    }

    public static BleDeviceListBean getFromCache() {
        BleDeviceListBean data;
        data = (BleDeviceListBean) FileUtils.readObject(FILE_CACHE);
        if (data != null) {
            mBleDeviceBean = data;
        }
        LogUtil.i("login---->getFromCache---" + (data == null));
        return data;
    }

    public void save() {
        mBleDeviceBean = this;
        FileUtils.cacheObject(FILE_CACHE, this);
        // FileSaveHandler.saveObject(FILE_CACHE, this);
        LogUtil.i("login---->save---" + FILE_CACHE);
    }

    private static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
        // FileSaveHandler.removeObject(FILE_CACHE);
    }

	public static void logout() {
		delete();
		mBleDeviceBean = null;
        TagAliasOperatorHelper.getInstance().cleanAlias();
	}


    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }



}
